home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Overload Trio 2
/
Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO
/
dir30
/
alsps.zip
/
ALSP4.DOC
< prev
next >
Wrap
Lisp/Scheme
|
1993-11-04
|
12KB
|
360 lines
This is Lesson 4 of a series of AutoLISP training exercises given
on the CompuServe ADESK Forum by the Autodesk, Inc. Training
Department.
AUTOLISP: THE AUTOCAD CALCULATOR
AutoCAD does not have a Sidekick-style pop-up calculator
to perform arithmetic operations and return the appropriate value
to an active AutoCAD command prompt, but it has an entire
programming language to calculate and store values and pass them
to AutoCAD. The comment that "AutoCAD doesn't have a built-in
calculator" is as true as it is meaningless, because it has an
entire built-in programming language, instead!
AutoLISP can be used at any time in the drawing editor. It can
be used at the command prompt, at the dimension prompt, and at
any prompt generated by any AutoCAD command. Its syntax is
probably not as easy to master initially as that of a simple
10-key calculator; however, it is much more powerful. And the
syntax, once learned, is simple and consistent.
RETURNING A VALUE TO THE COMMAND PROMPT
The value of an AutoLISP expression is passed from AutoLISP to
AutoCAD as the response to the active AutoCAD prompt.
If the command prompt is active, the value of an expression is
simply printed in the command prompt area. Any side effect that
occurs during the expression, such as a new symbol binding, is
irrelevant to the command prompt (as long as an AutoCAD command
is not called in the expression). For example, this form (a form
being any complex expression that is treated as program code
rather than data) binds the symbol X to the result of adding 4
and 6 and then passes the value 10 to AutoCAD, which simply
prints the result in the command prompt area.
Command: (setq x (+ 4 6))
10
There's no requirement to bind a symbol. Any form can be
evaluated at the command prompt simply to return a calculated
numeric value.
For example, this form returns the value of dividing the square
root of 50 by the real number 9.
Command: (/ (sqrt 50.0) 9.0)
0.785674
This form returns the value of multiplying the result of adding
2.5 and 6.3 by 24.0.
Command: (* (+ 2.5 6.3) 24.0)
211.2
RETURNING A VALUE TO AN AUTOCAD COMMAND
If an AutoCAD command is active, then evaluation of an AutoLISP form
returns the value to AutoCAD. The value becomes the response to
the current prompt of the AutoCAD command.
In this example, the ARRAY command receives as the number of
items to array the result of an AutoLISP form that divides 6.0
by 0.35; in other words, we don't know offhand how many items
will fit at 0.35 unit increments into a distance of 6.0 units, so
we ask AutoLISP to calculate the value for us and return the
answer to AutoCAD. Since the prompt from ARRAY accepts only
integer values rather than real numbers, we'll use the AutoLISP
(fix <real>) function to round down to the nearest integer.
Command: (/ 6.0 0.35)
17.1429
Command: (fix 17.1429)
17
Command: (fix (/ 6.0 0.35))
17
Command: LINE
From point: 1,1
To point: 5,5
To point: <return>
Command: ARRAY
Select objects: L
Select objects: <return>
Rectangular or Polar array (R/P): R
Number of rows (---) <1>: 1
Number of columns (|||) <1>: (fix (/ 6.0 0.35))
Distance between columns: 0.35
In this case, AutoCAD creates 17 items in the array.
The important point to consider is that an AutoLISP form has been
used interactively, in the middle of an AutoCAD command, to
calculate a value (in this case, an integer) that is subsequently
and automatically given to the command at that particular point
in its prompting sequence.
In this example, AutoLISP calculates the size of a fillet radius.
Command: FILLET
Polyline/Radius/<Select two lines>: R
Enter fillet radius <1.0>: (/ 28.0 8.0)
Since both arguments to (/) are reals, the result is a real, and
the size of the fillet radius is set to 3.5.
INTERACTIVE CALCULATION
Another method to pass values to an AutoCAD command is to use an
AutoLISP form that itself prompts the user for an arbitrary set
of values, and then performs some calculation on the values,
eventually returning the answer to AutoCAD. This is probably
easier to use than it is to explain.
Before we perform some complex calculation with user-prompted
values, we'll introduce two new AutoLISP functions: (getdist) and
(getpoint).
TWO NEW INTERACTIVE FUNCTIONS
(getdist) halts the execution of a form and allows the user to
specify a distance by typing in the value, or by selecting two
points; this is exactly the same mechanism used by every AutoCAD
prompt that calls for the user to specify a distance.
(getpoint) halts a form and allows the user to select a point,
using any of the standard point selection methods: screen
pointing, absolute, relative, or relative polar coordinate entry,
object snaps, coordinate filters, or lastpoint specification.
GETDIST
This example uses an optional prompt to the (getdist) function,
which prompts the user to enter a distance. Try using this form
at the command prompt and respond to it once by typing in a
value, and again by picking two points on the display.
The control characters "\n", or backslash and lower-case n, mean
newline. They make sure the rest of the prompt appears on a new
line in the command prompt area.
Command: (getdist "\nEnter a distance: ")
Enter a distance: <enter a number>
Command: (getdist "\nEnter a distance: ")
Enter a distance: <pick two points>
In both cases, (getdist) returns the distance entered by the user
as a real number.
GETPOINT
This example uses an optional prompt to the (getpoint) function,
which prompts the user to pick a point. Try using this form with
absolute coordinates, screen pointing, and object snaps.
Command: (getpoint "\nPoint: ")
Point: 1,1
(1.0 1.0 0.0)
AutoLISP returns the point as a list of three real numbers. 3-D
points are returned as a list of three reals; 2-D points as a
list of two reals. By default, all points chosen in Release 10
are fully 3-D, with X, Y, and Z values.
The first element in a point list is the X value, the second the
Y value, and the third the Z value.
This expression (1.0 1.0 0.0) is not a form, because it cannot be
evaluated. It contains no function as the first element of the
list. This is an example of AutoLISP using a list to store data
rather than program code.
Command: (getpoint "\nPoint: ")
Point: <pick point>
Command: LINE
From point: 1,1
To point: 5,5
To point: <return>
Command: (getpoint "\nPoint: ")
Point: MID
of <pick line>
(3.0 3.0 0.0)
PROMPTS ARE OPTIONAL
There's no law that says you have to include an optional string
argument to (getdist) or (getpoint) as a prompt for the user.
However, it's good practice to do so. Can you guess why?
Try using (getdist) and (getpoint) without the optional string
argument.
Command: (getdist)
?
Command: (getpoint)
?
SOME MORE CALCULATIONS
Computations might be a more accurate word, since a computer is
essentially a calculator with memory, and these calculations
require AutoLISP to remember distances and points.
In this example, variable X is bound to the list of three real
numbers that form the coordinate 1,1,0.
Command: (setq x (getpoint "\nPoint: "))
Point: 1,1
(1.0 1.0 0.0)
There's another way to bind X to a list of three real numbers
without using the (getpoint) function. Can you remember what it
is? It involves a function that tells AutoLISP to take the next
argument at face value (data) rather than to try to evaluate it
(a form)...
In this example, pick an arbitrary point anywhere on the screen
with the cursor.
Command: (setq x (getpoint))
<pick a point>
Next, draw a line starting at 0,0 and ending at the point stored
in variable X. How can you ask AutoLISP to use the current value
of a variable?
Command: LINE
From point: 0,0
To point: !x
To point: <return>
Binding variables to points is easy. It allows the user to store
as many points as needed by name in the current drawing editor
session, and refer to them by name anytime AutoCAD asks for a
point.
Command: CIRCLE
3P/2P/TTR/<Center point>: !x
Diameter/<Radius>: 1.0
Command: (setq origin (quote (0.0 0.0 0.0)))
(0.0 0.0 0.0)
Command: MOVE
Select objects: Last
Select objects: <return>
Base point or displacement: !x
Second point of displacement: !origin
The previous example should have moved the circle and placed its
center point at 0,0. Can you see why?
Here's another example of the ARRAY command. This time, AutoLISP
will calculate the distance between two points and use that to
help determine how many duplicate items it will create.
First, we'll build some arbitrary construction geometry.
Command: LINE
From point: 1.1143,5
To point: 8.9178,5
To point: <return>
Command: CIRCLE
3P/2P/TTR/<Center point>: 1.1143,5
Diameter/<Radius>: 0.5
Now, we'll use (getdist) to help provide facility to ARRAY that,
on the face of it, ARRAY appears not to have: interactively
calculate the number of items that must appear in the array based
only on the distance between each item and total distance in the
array! In this example, we'll space each item 0.5 units apart.
Command: ARRAY
Select objects: Last
Select objects: <return>
Rectangular or Polar array (R/P): R
Number of rows: 1
Number of columns: (fix (/ (getdist "\nDistance: ") 0.5))
Distance: END
of <pick start point of line>
Second point: END
of <pick end point of line>
Distance between columns: 0.5
Here's another example that fits a user-specified number of items
to a calculated distance.
Command: UNDO
Auto/Back/Control/End/Group/Mark/<number>: 1
Command: ARRAY
Select objects: Last
Select objects: <return>
Rectangular or Polar array (R/P): R
Number of rows: 1
Number of columns: 10
Distance between columns: (/ (getdist) 10)
END
of <pick start point of line>
Second point: END
of <pick end point of line>
Next week: Writing New AutoCAD Commands
There are no questions for this week's lesson. Please experiment
on your own, using AutoLISP expressions at the command prompt and
within AutoCAD commands to calculate and store answers for AutoCAD.
EXTRA CREDIT
One of the most interesting forms that comes with Release 10 is
the file REF.LSP, which you'll find on the Support Disks. It's
documented in Appendix A of the AutoLISP Programmer's Reference.
Examine the form in REF.LSP. See if you can determine how it
works! If you can, then you have a clear understanding of how
AutoCAD and AutoLISP work with each other. Clue #1: the (setvar)
function does the same thing as the AutoCAD SETVAR command.
ANSWERS TO LESSON 3
1. Reals are stored as double precision floating point numbers,
accurate to at least fourteen significant digits of precision.
2. No.
3. For code compatibility with DOS versions of AutoCAD.
4. The file cannot be accessed for read, write, or append
operations, nor can it be closed (AutoCAD itself will close the
file on exit from the drawing editor).
5. Selection-sets occupy temporary file slots, and as such
should be treated as precious resources. A variable should
always be bound to a newly-created selection-set, else it cannot
be accessed by a later operation within AutoLISP.
6. Up to six; however, don't use more than four unless you have
to.
7. A pointer to an entity record in the drawing editor session.
8. It can't; however, entity handles can be stored across
editing sessions and used to retrieve the same entities.
9. 100 characters; the limit of available heap space that can be
allocated as string space, in other words, the limit of available
memory.
10. The (type) function.